package com.example.fbneo_crc2;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileTool {


    /**
     * 复制文件
     *
     * @param source 输入文件
     * @param target 输出文件
     */
    public static boolean copy(File source, File target) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(source);
            fileOutputStream = new FileOutputStream(target);
            byte[] buffer = new byte[1024];
            int count=0;
            while ((count=fileInputStream.read(buffer)) > 0) {
                fileOutputStream.write(buffer,0,count);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
    public static boolean copy(String srcPath,String targetPath){
        return copy(new File(srcPath),new File(targetPath));
    }
    public static void deleteFolders(File folder) {
        if (folder != null && folder.isDirectory()) {
            for (File file : folder.listFiles()) {
                if (file.isDirectory()) {
                    deleteFolders(file);
                } else {
                    file.delete();
                }
            }

            folder.delete();
        }
    }


    public static String readFile(String file) throws Exception {
        FileInputStream out=null;
        ByteArrayOutputStream byteArrayOutputStream=null;
        byte[] bytes=new byte[1024*1024];
        int readSize=0;
        try{
            byteArrayOutputStream=new ByteArrayOutputStream();
            out=new FileInputStream(file);
            while ((readSize=out.read(bytes))!=-1){
                byteArrayOutputStream.write(bytes,0,readSize);
            }
            return new String(byteArrayOutputStream.toByteArray(),"utf-8");
        }finally {
            bytes=null;
            if(byteArrayOutputStream!=null)
                byteArrayOutputStream.close();
            if(out!=null)
                out.close();
        }
    }

    //读取Smali文件优化
    public static String readFileSmaliLine(String file) throws Exception {
        FileInputStream out=null;
        ByteArrayOutputStream byteArrayOutputStream=null;
        byte[] bytes=new byte[1024];
        int readSize=0;
        try{
            byteArrayOutputStream=new ByteArrayOutputStream();
            out=new FileInputStream(file);
            int index=0;
            while ((readSize=out.read(bytes))!=-1){
                byteArrayOutputStream.write(bytes,0,readSize);
                if(index==0){
                    String str=byteArrayOutputStream.toString();

                    if(!str.contains(".line ")){
//                        System.out.println("跳过");
                        return null;
                    }
                }
                index++;
            }
            return new String(byteArrayOutputStream.toByteArray(),"utf-8");
        }finally {
            bytes=null;
            if(byteArrayOutputStream!=null)
                byteArrayOutputStream.close();
            if(out!=null)
                out.close();
        }
    }





}
